﻿/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine.display {
	
	import flash.geom.Rectangle;
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import stencil.core.engine.Box;
	import stencil.core.engine.IComponent;
	import stencil.core.engine.errors.IllegalStateError;
	import stencil.core.engine.IComponent;
	import stencil.core.engine.ISceneObject;
	import stencil.core.engine.Position;
	import stencil.core.engine.Bounds;
	import stencil.core.engine.Box;
	import stencil.core.engine.IComponentList;
	import stencil.core.errors.ArgumentIsNullError;
	
	/**
	 * Sprite-based implementation of IDisplayObject interface.
	 * 
	 * @author	Max Zhuravkov
	 */
	public class SpriteObject extends Sprite implements IDisplayObject {
		
		private var _id:String;
		
		private var _flagTile:Boolean;
		
		private var _flagBackground:Boolean;
		
		private var _priority:int = 0;
		
		private var _displayState:DisplayState;
		
		private var _data:Object;
		
		private var _enabled:Boolean = true;
		
		private var _owner:ISceneObject;
		
		private var _disposed:Boolean;
		
		private var _flagStatic:Boolean;
		
		//----------------------------------------------------------------
		// Constructor
		//----------------------------------------------------------------
		
		public function SpriteObject() {
			super();
			this.init();
		}
		
		//----------------------------------------------------------------
		// Public methods
		//----------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public function set id(value:String):void {
			if (this._id && this._id == value) {
				return;
			}
			this._id = value;
		}
		
		public function get id():String{
			return this._id;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set priority(value:int):void {
			if (this._priority != value) {
				this._priority = value;
			}
		}
		
		public function get priority():int{
			return this._priority;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set displayState(value:DisplayState):void {
			this.checkState();
			this._displayState = value;
		}
		
		public function get displayState():DisplayState {
			return this._displayState;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function get content():DisplayObject {
			return this;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set flagTile(value:Boolean):void {
			this._flagTile = value;
		}
		
		public function get flagTile():Boolean {
			return this._flagTile;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set flagBackground(value:Boolean):void {
			this._flagBackground = value;
		}
		
		public function get flagBackground():Boolean {
			return this._flagBackground;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function get flagDisplayable():Boolean {
			return true;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set flagStatic(value:Boolean):void {
			if (this._flagStatic == value) {
				return;
			}
			this._flagStatic = value;
		}
		
		public function get flagStatic():Boolean {
			return this._flagStatic;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set enabled(value:Boolean):void {
			this.checkState();
			if (this._enabled == value) {
				return;
			}
			this._enabled = value;
			this.enabledChanged(value);
		}
		
		public function get enabled():Boolean {
			return this._enabled;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function set owner(value:ISceneObject):void {
			this.checkState();
			if (this._owner == value) {
				return;
			}
			if (this._owner) {
				this.ownerDetached(this._owner);
			}
			this._owner = value;
			if (this._owner) {
				this.ownerAttached(this._owner);
			}
		}
		
		public final function get owner():ISceneObject {
			return this._owner;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set data(value:Object):void {
			this.checkState();
			if (this._data) {
				this.dataDetached(this._data);
			}
			this._data = value;
			if (this._data) {
				this.dataAttached(this._data);
			}
		}
		
		public function get data():Object {
			return this._data;
		}
		
		//----------------------------------------------------------------
		// Public methods
		//----------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public function render(viewPort:Rectangle, time:Number):void {
			this.checkState();
		}
		
		/**
		 * @inheritDoc
		 */
		public final function dispose():void {
			this.releaseResources();
			this._disposed = true;
		}
		
		//----------------------------------------------------------------
		// Protected methods
		//----------------------------------------------------------------
		
		protected function init():void {
			
		}
		
		protected final function checkState():void {
			if (this._disposed) {
				throw new IllegalStateError("Object '" + this.id + "' has been disposed.");
			}
		}
		
		/**
		 * Called when this display object is attached to the given scene object.
		 * 
		 * @param	owner	the new owner of this component.
		 */
		protected function ownerAttached(owner:ISceneObject):void {
			
		}
		
		/**
		 * Called when this display object is detached from the given scene object.
		 * 
		 * @param	owner		the previous owner of this component.
		 */
		protected function ownerDetached(owner:ISceneObject):void {
			
		}
		
		/**
		 * Called when the given data object is attached to this display object.
		 * 
		 * @param	data	the data object.
		 */
		protected function dataAttached(data:Object):void {
			
		}
		
		/**
		 * Called when the given data object is detached from this display object.
		 * 
		 * @param	data	the data object.
		 */
		protected function dataDetached(data:Object):void {
			
		}
		
		/**
		 * Called when this display object is enabled or disabled.
		 * 
		 * @param	enabled		true if this component is enabled and false otherwise.
		 */
		protected function enabledChanged(enabled:Boolean):void {
			
		}
		
		/**
		 * Releases resources acquired by this display object.
		 */
		protected function releaseResources():void {
			
		}
	}
	
}